package org.fly.controller.wap.supplier;

import org.fly.bean.*;
import org.fly.bean.query.CustomerLevelQuery;
import org.fly.bean.query.ProductQuery;
import org.fly.bean.query.SCLRQuery;
import org.fly.bean.warpper.SCLRWarpper;
import org.fly.controller.AbstractController;
import org.fly.enums.Relation;
import org.fly.service.*;
import org.fly.utils.JsonUtils;
import org.fly.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by rongzhao.zhu on 2018/2/28.
 */
@Controller
@RequestMapping("/wap/supplier/product/")
public class WapProductController extends AbstractController {

    @Autowired
    ProductService productService;
    @Autowired
    SCLRService sclrService;
    @Autowired
    ProductNormService productNormService;
    @Autowired
    CustomerLevelService customerLevelService;
    @Autowired
    SupplierProductService supplierProductService;

    @RequestMapping(value = "queryExcludeProductPage", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryExcludeProductPage(ProductQuery query){
        Supplier supplier = (Supplier) UserContext.getUser();
        query.setRelation(Relation.exclude.name());
        query.setSupplierId(supplier.getId());
        List<Product> list = productService.list(query );
        long count = this.productService.count(query);
        Page page = new Page(query.getPage(),query.getSize());
        page.setData(list);
        page.setTotal(count);
        return JsonUtils.encode(createDataTip(page));
    }
    @RequestMapping(value = "queryHasProductPage", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryHasProductPage(ProductQuery query){
        Supplier supplier = (Supplier) UserContext.getUser(); 
        query.setRelation(Relation.has.name());
        query.setSupplierId(supplier.getId());
        List<Product> list = productService.list(query );
        long count = this.productService.count(query);
        Page page = new Page(query.getPage(),query.getSize());
        page.setData(list);
        page.setTotal(count);
        return JsonUtils.encode(createDataTip(page));
    }

    /**
     * 查询没有匹配的商品信息
     * @param productId
     * @return
     */
    @RequestMapping(value = "queryProductConfig" ,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String queryProductConfig(String productId){
        Supplier supplier = (Supplier) UserContext.getUser();
        Product  product = productService.findById(productId);
        CustomerLevelQuery query = new CustomerLevelQuery();
        query.setSupplierId(supplier.getId());
        List<CustomerLevel> levels = customerLevelService.list(query);
        ProductNorm productNorm= productNormService.findByProductId(productId);
        SupplierProduct supplierProduct =  supplierProductService.find(supplier.getId(),productId);
        Map<String,Object> result = new HashMap<>();
        result.put("product",product);
        result.put("levels",levels);
        result.put("productNorm",productNorm);
        result.put("supplierProduct",supplierProduct);
        return JsonUtils.encode(createDataTip(result));
    }

    @RequestMapping(value = "save", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String save(SupplierProduct bean, HttpServletRequest request){
        try {
            Supplier supplier = (Supplier) UserContext.getUser();
            bean.setSupplierId(supplier.getId());
            supplierProductService.save(bean);
            return toJSONString(SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return toExceptionString(e);
        }
    }


}
